//
//  UIDevice+DeviceType.m
//  mytest
//
//  Created by young on 15-1-22.
//  Copyright (c) 2015年 young. All rights reserved.
//
/**
    获取手机型号：
    iPhone4： iPhone3.1－iPhone3.3
    iPhone4S：iPhone4.1
    iPhone5： iPhone5.1－iPhone5.2
    iPhone5C：iPhone5.3－iPhone5.4
    iPhone5S：iPhone6.1－iPhone6.2
    iPhone6： iPhone7.1
    iPhone6＋：iPhone7.2
 
 */
#import "UIDevice+DeviceType.h"
#include "sys/types.h"
#include "sys/sysctl.h"

@implementation UIDevice (DeviceType)
- (NSString*)machine
{
    NSMutableDictionary *machineDict = [[NSMutableDictionary alloc] init];
    [machineDict setObject:@"iPhone4" forKey:@"iPhone3,1"];
    [machineDict setObject:@"iPhone4" forKey:@"iPhone3,2"];
    [machineDict setObject:@"iPhone4" forKey:@"iPhone3,3"];
    [machineDict setObject:@"iPhone4S" forKey:@"iPhone4,1"];
    [machineDict setObject:@"iPhone5" forKey:@"iPhone5,1"];
    [machineDict setObject:@"iPhone5" forKey:@"iPhone5,2"];
    [machineDict setObject:@"iPhone5C" forKey:@"iPhone5,3"];
    [machineDict setObject:@"iPhone5C" forKey:@"iPhone5,4"];
    [machineDict setObject:@"iPhone5S" forKey:@"iPhone6,1"];
    [machineDict setObject:@"iPhone5S" forKey:@"iPhone6,2"];
    [machineDict setObject:@"iPhone6" forKey:@"iPhone7,1"];
    [machineDict setObject:@"iPhone6 Plus" forKey:@"iPhone7,2"];
    
    size_t size;
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);
    char *name = (char*)malloc(size);
    sysctlbyname("hw.machine", name, &size, NULL, 0);
    NSString *machine = [[NSString alloc]initWithUTF8String:name];
    
    free(name);
    
    NSString *machineType = machineDict[machine];
    
    if (machineType) {
        return machineType;
    }else{
        return machine;
    }

}
@end
